import 'package:flutter/material.dart';

class TabBarControllerPage extends StatefulWidget {
  TabBarControllerPage({Key key}) : super(key: key);

  _TabBarControllerPageState createState() => _TabBarControllerPageState();
}

class _TabBarControllerPageState extends State<TabBarControllerPage> with SingleTickerProviderStateMixin {
  TabController _tabBarController;

  @override
  void initState(){ //生命周期函数 ，一加载就会调用
    super.initState();
    _tabBarController = new TabController(
      vsync:this,
      length:2
    );
    _tabBarController.addListener((){
      print(_tabBarController.index);
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
       appBar: AppBar(
         title: Text("_TabBarController"),
         bottom: TabBar(
           controller:this._tabBarController,
           tabs: <Widget>[
             Tab(text: "tab1"),
             Tab(text: "tab2"),
           ],
         ),
         
       ),
       body: TabBarView(
         controller:this._tabBarController,
         children: <Widget>[
           Center(child: Text("tab1"),),
           Center(child: Text("tab2"),)
         ],
       ),
    );
  }
}